<template>
<div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import {
    debounce
} from '@/utils'

export default {
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        },
    },

    mounted() {
        this.initChart()
        // if (this.autoResize) {
        //   this.__resizeHanlder = debounce(() => {
        //     if (this.chart) {
        //       this.chart.resize()
        //     }
        //   }, 100)
        //   window.addEventListener('resize', this.__resizeHanlder)
        // }

        // // 监听侧边栏的变化
        // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
        // sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        // if (this.autoResize) {
        //   window.removeEventListener('resize', this.__resizeHanlder)
        // }

        // const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
        // sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

        this.chart.dispose()
        this.chart = null;

    },
    methods: {
        setOptions({
            dataValue,
            dataName,
            dataTitle
        } = {}) {
            console.log(dataValue, '1', dataName, '2', dataTitle, '3')
            this.chart.setOption({
                xAxis: {
                    data: dataName,
                    boundaryGap: true,
                    axisTick: {
                        show: false
                    }
                },
                title: {
                    text: '',
                    subtext: '',
                    x: 'right',
                    textStyle: {
                        fontSize: 12,
                        fontWeight: 'bolder',
                    },
                },
                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    axisTick: {
                        show: false
                    },
                    // min:'3200'
                    scale: true
                },
                legend: {
                    data: dataTitle
                },
                series: this.setData(dataTitle, dataValue)
            })
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.chartData)
        },

        /*** 封装一个函数， 对 后台传来的数据进行处理方法设置。*/
        setData(dataTitle, dataValue) {
            var seriesArr = [];
            for (var i = 0; i < dataTitle.length; i++) {
                var object = new Object();
                object.name = dataTitle[i];
                object.type = 'line';
                object.stack = '总量';
                object.data = dataValue[i];

                seriesArr.push(object);
            }
            console.log(seriesArr);
            return seriesArr;
        }
    }
}
</script>
